package com.edu.entity;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/*
 * 工具类，将部分功能整合
 */
public class Tool {
	//将数字id转换为对应的分类字符串
	public String typeIntToString(int i) {
		String out = "";
		if(i == 1) {
			out = "数码电子";
		}else if(i == 2) {
			out = "家用电器";
		}else if(i == 3) {
			out = "服装鞋包";
		}else if(i == 4) {
			out = "家具家居";
		}else if(i == 5) {
			out = "母婴儿童";
		}else if(i == 6) {
			out = "运动户外";
		}else if(i == 7) {
			out = "美妆个护";
		}else if(i == 8) {
			out = "书籍音像";
		}else if(i == 9) {
			out = "珠宝首饰";
		}else if(i == 10) {
			out = "实用五金";
		}else if(i == 11) {
			out = "爱好潮玩";
		}else if(i == 12) {
			out = "生活用品";
		}
		return out;
	}
	//反向
	public int typeStringToInt(String type) {
	    if(type == null) {
	        return 0;  // 或者返回-1表示无效输入
	    }
	    
	    switch(type) {
	        case "数码电子": return 1;
	        case "家用电器": return 2;
	        case "服装鞋包": return 3;
	        case "家具家居": return 4;
	        case "母婴儿童": return 5;
	        case "运动户外": return 6;
	        case "美妆个护": return 7;
	        case "书籍音像": return 8;
	        case "珠宝首饰": return 9;
	        case "实用五金": return 10;
	        case "爱好潮玩": return 11;
	        case "生活用品": return 12;
	        default: return 0;  // 或者返回-1表示未找到匹配项
	    }
	}
	//将数字id转换为对应的状态字符串
	public String statIntToString(int i) {
		String out = "";
		if(i == 1) {
			out = "全新";
		}else if(i == 2) {
			out = "99新";
		}else if(i == 3) {
			out = "95新";
		}else if(i == 4) {
			out = "9成新";
		}else if(i == 5) {
			out = "8成新";
		}else if(i == 6) {
			out = "7成新或以下";
		}
		return out;
	}
	//反向
	public int statStringToInt(String status) {
	    if (status == null) {
	        return 0;  // 或返回 -1 表示无效输入
	    }
	    
	    switch (status) {
	        case "全新": return 1;
	        case "99新": return 2;
	        case "95新": return 3;
	        case "9成新": return 4;
	        case "8成新": return 5;
	        case "7成新或以下": return 6;
	        default: return 0;  // 或返回 -1 表示未找到匹配项
	    }
	}
	public String intToSex(int i) {
		switch(i) {
		case 1:return "男";
		case 2:return "女";
		default:return "女";
		}
	}
	public int sexToInt(String sex) {
		switch(sex) {
		case "男":return 1;
		case "女":return 2;
		default:return 2;
		}
	}
	public String errorCodeToString(int i) {
		switch(i) {
		case 1062:return "用户名重复";
		default:return "";
		}
	}
	public String orderTypeToSql(int i) {
		switch(i) {
		case 2:return" order by goodprice asc";
		case 1:return" order by goodprice desc";
		default:return "";
		}
	}
	public String tradetIntToString(int i) {
		switch(i) {
		case 1:return"邮寄";
		case 2:return"自提";
		case 3:return"线下交易";
		default:return "";
		}
	}
	public String freightIntToString(int i) {
		switch(i) {
		case 1:return"卖家包邮";
		case 2:return"买家承担";
		default:return "";
		}
	}
	public static int[] calculateDateDifference(Date inputDate) {
        // 转换为java.time.LocalDate
        LocalDate currentDate = LocalDate.now();
        LocalDate inputLocalDate = inputDate.toInstant()
                                      .atZone(ZoneId.systemDefault())
                                      .toLocalDate();
        
        // 使用Period计算差值
        Period period = Period.between(inputLocalDate, currentDate);
        
        return new int[]{
            period.getYears(),
            period.getMonths(),
            period.getDays()
        };
    }
private static final Map<String, String> provinceToRegion = new HashMap<>();
    
    static {
        // 华东地区（8个）
        provinceToRegion.put("上海", "华东");
        provinceToRegion.put("江苏", "华东");
        provinceToRegion.put("浙江", "华东");
        provinceToRegion.put("安徽", "华东");
        provinceToRegion.put("福建", "华东");
        provinceToRegion.put("江西", "华东");
        provinceToRegion.put("山东", "华东");
        provinceToRegion.put("台湾", "华东");
        
        // 华北地区（5个）
        provinceToRegion.put("北京", "华北");
        provinceToRegion.put("天津", "华北");
        provinceToRegion.put("河北", "华北");
        provinceToRegion.put("山西", "华北");
        provinceToRegion.put("内蒙古", "华北");
        
        // 华南地区（5个）
        provinceToRegion.put("广东", "华南");
        provinceToRegion.put("广西", "华南");
        provinceToRegion.put("海南", "华南");
        provinceToRegion.put("香港", "华南");
        provinceToRegion.put("澳门", "华南");
        
        // 华西地区（12个）
        provinceToRegion.put("重庆", "华西");
        provinceToRegion.put("四川", "华西");
        provinceToRegion.put("贵州", "华西");
        provinceToRegion.put("云南", "华西");
        provinceToRegion.put("西藏", "华西");
        provinceToRegion.put("陕西", "华西");
        provinceToRegion.put("甘肃", "华西");
        provinceToRegion.put("青海", "华西");
        provinceToRegion.put("宁夏", "华西");
        provinceToRegion.put("新疆", "华西");
        provinceToRegion.put("湖北", "华西");
        provinceToRegion.put("湖南", "华西");
        
        // 东北地区（3个）如果需要可以单独分类
        provinceToRegion.put("辽宁", "东北");
        provinceToRegion.put("吉林", "东北");
        provinceToRegion.put("黑龙江", "东北");
    }
    
    /**
     * 判断省份所属大区
     * @param province 省份名称（如"江苏"、"广东"等）
     * @return 大区名称（华东/华北/华南/华西/东北），未知省份返回"未知"
     */
    public static String getRegion(String province) {
        return provinceToRegion.getOrDefault(province, "未知");
    }
    
    public double feeCount(String sellerAddress,String buyerAddress) {
    	double baseFee = 10.0;
    	double fee = 0.0;
    	System.out.println(getRegion(sellerAddress)+"到"+getRegion(buyerAddress));
    	if(getRegion(sellerAddress).equals("华北")) {
    		switch(getRegion(buyerAddress)) {
    		case "华北":fee = baseFee * 0.5;
    		break;
    		case "华南":fee = baseFee * 2;
    		break;
    		case "华西":fee = baseFee * 1.5;
    		break;
    		case "华东":fee = baseFee * 1.5;
    		break;
    		default:fee = baseFee * 1.0;
    		}
    		return fee;
    	}else if(getRegion(sellerAddress).equals("华南")) {
    		switch(getRegion(buyerAddress)) {
    		case "华北":fee = baseFee * 2.0;
    		break;
    		case "华南":fee = baseFee * 0.5;
    		break;
    		case "华西":fee = baseFee * 1.5;
    		break;
    		case "华东":fee = baseFee * 1.5;
    		break;
    		default:fee = baseFee * 1.0;
    		}
    		return fee;
    	}else if(getRegion(sellerAddress).equals("华东")) {
    		switch(getRegion(buyerAddress)) {
    		case "华西":fee = baseFee * 2.0;
    		break;
    		case "华东":fee = baseFee * 0.5;
    		break;
    		case "华北":fee = baseFee * 1.5;
    		break;
    		case "华南":fee = baseFee * 1.5;
    		break;
    		default:fee = baseFee * 1.0;
    		}
    		return fee;
    	}else if(getRegion(sellerAddress).equals("华西")) {
    		switch(getRegion(buyerAddress)) {
    		case "华东":fee = baseFee * 2.0;
    		break;
    		case "华西":fee = baseFee * 0.5;
    		break;
    		case "华北":fee = baseFee * 1.5;
    		break;
    		case "华南":fee = baseFee * 1.5;
    		break;
    		default:fee = baseFee * 1.0;
    		}
    		return fee;
    	}else {
    		return baseFee;
    	}
    	
    	
    }
	
}
